package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.core.util.PageUtil;
import com.hbnu.system.mapper.FundsConditionMapper;
import com.hbnu.system.model.dto.FundsConditionPageDTO;
import com.hbnu.system.model.entity.FundsCondition;
import com.hbnu.system.service.FundsConditionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Service
public class FundsConditionServiceImpl extends ServiceImpl<FundsConditionMapper, FundsCondition> implements FundsConditionService {

    public boolean addnewFundsCondition(FundsCondition fundsCondition){
        return baseMapper.insert(fundsCondition)==1;
    }
    @Override
    public IPage<FundsCondition> pageCondition(FundsConditionPageDTO searchPageDTO) {
        if(searchPageDTO.getProjectType().equals("横向项目")) {
            IPage<FundsCondition> page = PageUtil.getPage(searchPageDTO.getPageNum(), searchPageDTO.getPageSize());
            LambdaQueryWrapper<FundsCondition> wrapper = Wrappers.lambdaQuery(FundsCondition.class)
                    .eq(FundsCondition::getProjectType,"横向项目");
            // 检查 projectName 是否为空或空白
            if (StrUtil.isNotBlank(searchPageDTO.getProjectName())) {
                // 如果 projectName 不为空或空白，添加模糊查询条件
                wrapper.like(FundsCondition::getProjectName, searchPageDTO.getProjectName());
            }
            IPage<FundsCondition> FundsconditionIPage = baseMapper.selectPage(page, wrapper);
//        IPage<PaperVO> taskVOIPage = iBeanService.copyPageProperties(paperIPage, PaperVO.class);
            List<FundsCondition> records = FundsconditionIPage.getRecords();
            List<FundsCondition> FundsConditionS = new ArrayList<>();

            int count = 1;
            int start = searchPageDTO.getPageSize() * (searchPageDTO.getPageNum() - 1) + 1;
            int end = searchPageDTO.getPageNum() * searchPageDTO.getPageSize();
            Collections.reverse(records);
            for (FundsCondition record : records) {
                if (count >= start && count <= end) {
                    FundsConditionS.add(this.getFundsCondition(record.getFundsConditionID()));
                }
                count += 1;
            }
//            Collections.reverse(FundsConditionS);
            FundsconditionIPage.setRecords(FundsConditionS);
            FundsconditionIPage.setTotal(FundsConditionS.size());
            return FundsconditionIPage;
        }else if(searchPageDTO.getProjectType().equals("纵向项目")){
            IPage<FundsCondition> page = PageUtil.getPage(searchPageDTO.getPageNum(), searchPageDTO.getPageSize());
            LambdaQueryWrapper<FundsCondition> wrapper = Wrappers.lambdaQuery(FundsCondition.class)
                    .eq(FundsCondition::getProjectType,"纵向项目");
            IPage<FundsCondition> FundsconditionIPage = baseMapper.selectPage(page, wrapper);
//        IPage<PaperVO> taskVOIPage = iBeanService.copyPageProperties(paperIPage, PaperVO.class);
            List<FundsCondition> records = FundsconditionIPage.getRecords();
            List<FundsCondition> FundsConditionS = new ArrayList<>();

            int count = 1;
            int start = searchPageDTO.getPageSize() * (searchPageDTO.getPageNum() - 1) + 1;
            int end = searchPageDTO.getPageNum() * searchPageDTO.getPageSize();
            Collections.reverse(records);
            for (FundsCondition record : records) {
                if (count >= start && count <= end) {
                    FundsConditionS.add(this.getFundsCondition(record.getFundsConditionID()));
                }
                count += 1;
            }
//            Collections.reverse(FundsConditionS);
            FundsconditionIPage.setRecords(FundsConditionS);
            FundsconditionIPage.setTotal(FundsConditionS.size());
            return FundsconditionIPage;
        }else{
            throw new BaseException("查询项目类型出错");
        }
    }
    @Override
    public FundsCondition getFundsCondition(String Fundsconditionid) {
       FundsCondition fundsCondition = super.getOne(Wrappers.lambdaQuery(FundsCondition.class)
                .eq(FundsCondition::getFundsConditionID, Fundsconditionid));
        if(Objects.isNull(fundsCondition)){
            throw new BaseException("经费编号异常!未查找到该经费");
        }
        return fundsCondition;
    }
}
